package com.oscar.ehome.view.setview;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.AppManager;
import com.oscar.ehome.base.C;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.loginbean.CodeBean;
import com.oscar.ehome.bean.loginbean.UserBean;
import com.oscar.ehome.bean.loginbean.logincompanyBean;
import com.oscar.ehome.bean.pagebean.DiaoChaInfo;
import com.oscar.ehome.bean.pagebean.StudyDetialBean;
import com.oscar.ehome.presenter.LoginPresenter;
import com.oscar.ehome.presenter.SetPresenter;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.VersionUtil;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.loginview.view.LoginView;
import com.oscar.ehome.view.pageview.BannerWebActivity;
import com.oscar.ehome.view.welcomeview.LeadActivity;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class SettingActivity extends BasesActivity<LoginView, LoginPresenter> implements LoginView {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 服务协议
     */
    @BindView(R.id.useing_guide)
    RelativeLayout useingGuide;
    /**
     * 隐私条款
     */
    @BindView(R.id.about_usg)
    RelativeLayout aboutUsg;
    /**
     * 版本更新
     */
    @BindView(R.id.set_update)
    RelativeLayout setUpdate;
    /**
     * 退出登录
     */
    @BindView(R.id.setting_back)
    Button settingBack;
    /**
     * 关于我们
     */
    @BindView(R.id.about)
    RelativeLayout about;
    /**
     * title布局
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     * 联系客服
     */
    @BindView(R.id.set_kefu)
    RelativeLayout setKefu;
    /**
     * 引导页
     */
    @BindView(R.id.set_trait)
    RelativeLayout set_trait;
    /**
     * 版本号
     */
    @BindView(R.id.version_text)
    TextView versionText;

    SetPresenter setPresenter;

    private LoginPresenter loginPresenter;
    String phone = "";//电话号码


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        setColor();
        initView();

    }

    @Override
    protected LoginPresenter createPresenter() {
        loginPresenter = new LoginPresenter(this);
        return loginPresenter;
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(SettingActivity.this).getSystemConfig().getColor()));
        TintManager.setTint(settingBack, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
    }

    /**
     * 初始化页面
     */
    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("设置");
        versionText.setText(VersionUtil.getVersionName(SettingActivity.this));
        loginPresenter.kefu();
    }


    @OnClick({R.id.title_layout_left, R.id.useing_guide, R.id.about_usg, R.id.set_update, R.id.setting_back, R.id.about,
            R.id.set_kefu, R.id.set_trait})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left:
                finish();
                break;
            case R.id.about://关于我们
                Intent intent_about = new Intent(SettingActivity.this, BannerWebActivity.class);
                intent_about.putExtra("contentsrc", C.about_url);
                intent_about.putExtra("type", "about");
                intent_about.putExtra("name", "");
                intent_about.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent_about);
                break;
            case R.id.useing_guide://服务协议
                Intent intent_guide = new Intent(SettingActivity.this, BannerWebActivity.class);
                intent_guide.putExtra("contentsrc", C.sever_url);
                intent_guide.putExtra("type", "server");
                intent_guide.putExtra("name", "");
                intent_guide.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent_guide);
                break;
            case R.id.about_usg://隐私条款
                Intent intent_usg = new Intent(SettingActivity.this, BannerWebActivity.class);
                intent_usg.putExtra("contentsrc", C.privacy_url);
                intent_usg.putExtra("type", "privacy");
                intent_usg.putExtra("name", "");
                intent_usg.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent_usg);
                break;
            case R.id.set_update://更新
                if (Build.VERSION.SDK_INT >= 23) {
                    String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
                    if (EasyPermissions.hasPermissions(SettingActivity.this, perms)) {//检查是否获取该权限
                        CommonUtils.checkupdate = 1;
                        CheckUpdateManger manager = new CheckUpdateManger(SettingActivity.this);
                        // 检查软件更新
                        manager.checkUpdate();
                    } else {
                        //第二个参数是被拒绝后再次申请该权限的解释
                        //第三个参数是请求码
                        //第四个参数是要申请的权限
                        //第二个参数是被拒绝后再次申请该权限的解释
                        //第三个参数是请求码
                        //第四个参数是要申请的权限
                        //EasyPermissions.requestPermissions(this, "请开启手机存储权限！", 0, perms);

                        if (EasyPermissions.somePermissionPermanentlyDenied(this, Arrays.asList(perms))) {
                            new AppSettingsDialog.Builder(SettingActivity.this, "为了您能方便使用，请开启存储权限！")
                                    .setTitle("提示")
                                    .setPositiveButton("去设置")
                                    .setNegativeButton("取消", null)
                                    .setRequestCode(0)
                                    .build()
                                    .show();
                        }
                    }
                } else {
                    CommonUtils.checkupdate = 1;
                    CheckUpdateManger manager = new CheckUpdateManger(SettingActivity.this);
                    // 检查软件更新
                    manager.checkUpdate();
                }
                break;
            case R.id.setting_back://退出登录
                this.getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
                SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
                SystemConfigFactory.getInstance(SettingActivity.this).getSystemConfig().setIsFirst(true);
                Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                startActivity(intent);
                AppManager appManager = AppManager.getAppManager();
                appManager.finishAllActivity();
                finish();
                break;
            case R.id.set_kefu://联系客服
                if (phone == null || phone.equals("")) {
                    loginPresenter.kefu();
                } else {
                    Intent intent_kefu = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + phone);
                    intent_kefu.setData(data);
                    startActivity(intent_kefu);
                }
                break;
            case R.id.set_trait://引导页
                Intent intent_yd = new Intent(SettingActivity.this, LeadActivity.class);
                intent_yd.putExtra("lead", 2);
                startActivity(intent_yd);
                break;
        }
    }

    @Override
    public void navigateToHome(UserBean result) {

    }

    @Override
    public void getDiaocharesult(DiaoChaInfo result) {

    }

    @Override
    public void getbaojianjufile(StudyDetialBean result) {

    }

    @Override
    public void code(logincompanyBean result) {

    }

    @Override
    public void phoneNumber(CodeBean result) {
        if (result != null && !"".equals(result)) {
            phone = result.getPhone();
        }
    }

    @Override
    public void erroy() {

    }

    @Override
    public void erry(String result) {

    }
}
